﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyManagement.Models;

namespace PartyManagement.Controllers
{
    public class AttendController : Controller
    {
        private PartyManagementContext db = new PartyManagementContext();

        //
        // GET: /Attend/

        public ActionResult Index()
        {
            var attends = db.Attends.Include(a => a.UserProfile).Include(a => a.Event);
            return View(attends.ToList());
        }

        //
        // GET: /Attend/Details/5

      

        //
        // GET: /Attend/Create

        //public ActionResult Create()
        //{
        //    ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName");
        //    ViewBag.eventID = new SelectList(db.Events, "eventID", "eventName");
        //    return View();
        //}

        //
        // POST: /Attend/Create

        //[HttpPost]
        //public ActionResult Create(Attend attend)
        //{
           


        //    if (ModelState.IsValid)
        //    {
        //        db.Attends.Add(attend);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName", attend.userID);
        //    ViewBag.eventID = new SelectList(db.Events, "eventID", "eventName", attend.eventID);
        //    return View(attend);
        //}


      

        //
        // GET: /Attend/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Attend attend = db.Attends.Find(id);
            if (attend == null)
            {
                return HttpNotFound();
            }
            ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName", attend.userID);
            ViewBag.eventID = new SelectList(db.Events, "eventID", "eventName", attend.eventID);
            return View(attend);
        }

        //
        // POST: /Attend/Edit/5

        [HttpPost]
        public ActionResult Edit(Attend attend)
        {
            if (ModelState.IsValid)
            {
                db.Entry(attend).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.userID = new SelectList(db.UserProfiles, "UserId", "UserName", attend.userID);
            ViewBag.eventID = new SelectList(db.Events, "eventID", "eventName", attend.eventID);
            return View(attend);
        }

        //
        // GET: /Attend/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Attend attend = db.Attends.Find(id);
            if (attend == null)
            {
                return HttpNotFound();
            }
            return View(attend);
        }

        //
        // POST: /Attend/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Attend attend = db.Attends.Find(id);
            db.Attends.Remove(attend);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Invited()
        {

            List<Event> even=null;
             int userId = Convert.ToInt32(Session["userID"]);

            if(userId==0) 
                return RedirectToAction("Login","Account");
            else even = db.Events.Where(p=>p.Attends.FirstOrDefault(l=>l.userID==userId) !=null).ToList();

            return View(even);

        }
    }
}